package es.esa.integral;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;

import com.example.integral.R;

import java.io.IOException;

/**
 * Created by esalazar on 4/25/14.
 */
public class ExposureMapsActivity extends Activity {
    private EditText sourceName;
    private EditText ra;

    private EditText dec;

    private Button simbadButton;
    private Button exposureButton;
    private JSONServiceClient.ExposureInstruments exposure;
    private TextView textCoordinates;
    private TextView textExposures;
    private SimbadService simbadService = SimbadService.getInstance();
    private SimbadInformationBean info;
    private JSONServiceClient mJSONServiceClient = JSONServiceClient.getInstance() ;




    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exposure_map);
        simbadButton = (Button) findViewById(R.id.button_simbad);
        sourceName = (EditText) findViewById(R.id.source_name);
        ra = (EditText) findViewById(R.id.ra);
        dec = (EditText) findViewById(R.id.dec);

        simbadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("readSchedule", "onClick");


                new AsyncTask<Void, Void, Void>(){

                    @Override
                    protected Void doInBackground(Void... params){
                        info = simbadService.useRESTWebService(sourceName.getText().toString());

                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result){
                       // textCoordinates = (TextView) findViewById(R.id.coordinates);
                        if (info.getObjectId().equals("-1")) {
                            textCoordinates.setText("Source name Unknown");

                        } else {
                            String text = "RA: " + info.getPosition().ra + ", DEC: "+ info.getPosition().dec;
                            //textCoordinates.setText(text) ;
                            ra.setText( Double.valueOf(info.getPosition().ra).toString() );
                            dec.setText( Double.valueOf(info.getPosition().dec).toString() );
                        }
;
                    }
                }.execute();

            }
        });
        exposureButton =  (Button) findViewById(R.id.button_exposure);
        exposureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("readSchedule", "onClick");


                new AsyncTask<Void, Void, Void>(){

                    @Override
                    protected Void doInBackground(Void... params){
                        try {
                            exposure = mJSONServiceClient.getExposure(Double.valueOf(ra.getText().toString()), Double.valueOf(dec.getText().toString()));
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result){
                        textExposures = (TextView) findViewById(R.id.textExposure);
                        if (exposure == null) {
                            textExposures.setText("Impossible to calculate Exposure");

                        } else {
                            String text = "SPI: " + exposure.getSpiFormatted() + " ks, IBIS: "+ exposure.getIbisFormatted() + " ks";
                            textExposures.setText(text) ;
                        }

                    }
                }.execute();

            }
        });



    }




}